from masoniteorm.schema import Schema

from application.config import appSettings
from application.config.database import DB
from ..base import BaseModel


class Download(BaseModel):
    __table__ = appSettings.db_prefix + 'downloads'

    __casts__ = {
        'params': 'json',
    }

    @staticmethod
    def create_table():
        table_name = __class__.__table__
        # 动态创建表
        mSchema = Schema(connection=__class__.__connection__, connection_details=DB.get_connection_details())
        # 如果没有表则创建
        if not mSchema.has_table(table_name):
            with mSchema.create(table_name) as table:
                table.big_increments('id').primary()
                table.integer('type', 11).default(1).index().comment('类型 1 导出码包')
                table.integer('status', 11).default(0).index().comment('状态 0 待处理 1 处理中 2 已完成 3 失败')
                table.text('params').comment('参数')
                table.integer('position', 11).default(1).index().comment('存储位置 0 local 1 Qiniu 2 OSS')
                table.integer('is_public', 11).default(1).index().comment('公开 1 是 0 否')
                table.string('name', 255).nullable().comment('文件名称')
                table.string('path', 255).nullable().comment('文件路径')
                table.string('url', 255).nullable().comment('文件url前缀')
                table.string('remarks', 255).nullable().comment('备注')
                table.integer('admin_id', 11).default(0).index().comment('管理员ID')
                # created_at/updated_at
                table.timestamp('created_at', True, True)
                table.timestamp('updated_at', True, True)


__all__ = [
    'Download',
]
